<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="cn">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />






















<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=6.0.0" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.0.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=6.0.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=6.0.0">


  <link rel="mask-icon" href="/images/logo.svg?v=6.0.0" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta property="og:type" content="website">
<meta property="og:title" content="categories">
<meta property="og:url" content="http://yoursite.com/categories/index.html">
<meta property="og:site_name" content="hill&#39;s blog">
<meta property="og:locale" content="cn">
<meta property="article:published_time" content="2017-07-10T08:36:26.000Z">
<meta property="article:modified_time" content="2021-02-25T04:04:48.836Z">
<meta property="article:author" content="heyaojia">
<meta name="twitter:card" content="summary">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '6.0.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/categories/"/>





  <title>Nosql/Redis-基础 | hill's blog</title>
  








<meta name="generator" content="Hexo 4.2.1"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="cn">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">hill's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            Archives
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2021/04/01/Nosql/Redis-%E5%9F%BA%E7%A1%80/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="heyaojia">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="hill's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Nosql/Redis-基础</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2021-04-01T10:48:01+08:00">2021-04-01</time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="Redis-基础"><a href="#Redis-基础" class="headerlink" title="Redis-基础"></a>Redis-基础</h3><hr>
<h4 id="一、准备"><a href="#一、准备" class="headerlink" title="一、准备"></a>一、准备</h4><h5 id="1-MacOS安装"><a href="#1-MacOS安装" class="headerlink" title="1. MacOS安装"></a>1. MacOS安装</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">### 下载包解压后，cd到目录</span></span><br><span class="line">1. make</span><br><span class="line">2. make install</span><br><span class="line">3. mkdir myconfig</span><br><span class="line">4. cp redis.conf myconfig <span class="comment">### 备份一份配置文件，修改相关参数</span></span><br><span class="line">5. 修改myconfig/redis.conf中的daemonize改为yes<span class="comment">### 后台启动</span></span><br><span class="line">6. redis-server myconfig/redis.conf</span><br><span class="line">7. ps -ef |grep redis</span><br><span class="line">8. redis-cli -h localhost -p 6379</span><br><span class="line">9. redis客户端中输入PING</span><br></pre></td></tr></table></figure>

<a id="more"></a>

<h5 id="2-官方自带的测试"><a href="#2-官方自带的测试" class="headerlink" title="2. 官方自带的测试"></a>2. 官方自带的测试</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-bechmark -p 6379 -c 100 -n 100000</span><br></pre></td></tr></table></figure>

<table>
<thead>
<tr>
<th align="center">参数命令</th>
<th align="center">参数含义</th>
<th align="center">额外</th>
</tr>
</thead>
<tbody><tr>
<td align="center">p</td>
<td align="center">端口</td>
<td align="center"></td>
</tr>
<tr>
<td align="center">c</td>
<td align="center">并发数</td>
<td align="center"></td>
</tr>
<tr>
<td align="center">n</td>
<td align="center">请求数</td>
<td align="center"></td>
</tr>
</tbody></table>
<p><img src="D:%5Chill%5Cmyhexo%5Cpublic%5Cimages%5Cimage-20210330131305759.png" alt="image-20210330131305759"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">### 参数说明：</span></span><br><span class="line">1000000个reques请求在2.57s发完</span><br><span class="line">100个并发客户端</span><br><span class="line">3个字节每次写入</span><br><span class="line">只有一台服务器（单机性能）</span><br></pre></td></tr></table></figure>

<p><img src="D:%5Chill%5Cmyhexo%5Csource%5Cimages%5CRedis-%E5%9F%BA%E7%A1%80%5Cimage-20210330131552810.png" alt="image-20210330131552810"></p>
<p><img src="D:%5Chill%5Cmyhexo%5Csource%5Cimages%5CRedis-%E5%9F%BA%E7%A1%80%5Cimage-20210330131731298.png" alt="image-20210330131731298"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">### 1. 所有请求在6ms处理完毕</span></span><br><span class="line"><span class="comment">### 2. 每秒41305个请求</span></span><br></pre></td></tr></table></figure>

<h4 id="二、Redis数据类型-Redis文档"><a href="#二、Redis数据类型-Redis文档" class="headerlink" title="二、Redis数据类型 Redis文档"></a>二、Redis数据类型 <a href="www.baidu.com">Redis文档</a></h4><h5 id="1-五大基本数据类型"><a href="#1-五大基本数据类型" class="headerlink" title="1. 五大基本数据类型"></a>1. 五大基本数据类型</h5><p><strong><em>String：字符串:</em></strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">########## MSET、MGET ##########</span></span><br><span class="line">127.0.0.1:6379&gt; MSET k1 v1 k2 v2 k3 v3 <span class="comment">#### 同时设置多个值</span></span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; MGET k1 v1 k2 v2 k3 v3 <span class="comment">#### 同时取多个值</span></span><br><span class="line">1) <span class="string">"v1"</span></span><br><span class="line">2) <span class="string">"v2"</span></span><br><span class="line">3) <span class="string">"v3"</span></span><br><span class="line">127.0.0.1:6379&gt; MSETNX k1 v1 k4 v4  <span class="comment">#### 原子性操作</span></span><br><span class="line">(<span class="built_in">integer</span>) 0 <span class="comment">### 两个都失败了</span></span><br><span class="line"></span><br><span class="line"><span class="comment">########## 巧妙的设计key ##########</span></span><br><span class="line"><span class="comment">###user：userid：filed</span></span><br><span class="line">normal</span><br><span class="line"><span class="built_in">set</span> user:1 &#123;name:zhangsan, age:3&#125; <span class="comment">### key = user:1 json保存对象</span></span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; mset user:1:name zhangsan user:1:age 12</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; MGET user:1:name user:1:age</span><br><span class="line">1) <span class="string">"zhangsan"</span></span><br><span class="line">2) <span class="string">"12"</span></span><br><span class="line"><span class="comment">########## 先get后set ##########</span></span><br><span class="line">127.0.0.1:6379&gt; getset db redis</span><br><span class="line">(nil)</span><br><span class="line">127.0.0.1:6379&gt; get db</span><br><span class="line"><span class="string">"redis"</span></span><br><span class="line">127.0.0.1:6379&gt; getset db mongodb</span><br><span class="line"><span class="string">"redis"</span></span><br></pre></td></tr></table></figure>

<p><strong><em>List: 列表</em></strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">########## 例子数据准备 ##########</span></span><br><span class="line"><span class="comment">########## 插入 ##########</span></span><br><span class="line">127.0.0.1:6379&gt; LPUSH list <span class="string">"hello"</span></span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; LPUSH list <span class="string">"hello1"</span></span><br><span class="line">(<span class="built_in">integer</span>) 2</span><br><span class="line">127.0.0.1:6379&gt; LPUSH list <span class="string">"hello2"</span></span><br><span class="line">(<span class="built_in">integer</span>) 3</span><br><span class="line">127.0.0.1:6379&gt; LPUSH list <span class="string">"hello3"</span></span><br><span class="line">(<span class="built_in">integer</span>) 4</span><br><span class="line"><span class="comment">########## 读取列表 ##########</span></span><br><span class="line">127.0.0.1:6379&gt; LPUSH list one</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; LPUSH list two</span><br><span class="line">(<span class="built_in">integer</span>) 2</span><br><span class="line">127.0.0.1:6379&gt; LPUSH list three</span><br><span class="line">(<span class="built_in">integer</span>) 3</span><br><span class="line">127.0.0.1:6379&gt; LRANGE list 0 -1 <span class="comment">### 范围读取</span></span><br><span class="line">1) <span class="string">"three"</span></span><br><span class="line">2) <span class="string">"two"</span></span><br><span class="line">3) <span class="string">"one"</span></span><br><span class="line">127.0.0.1:6379&gt; LRANGE list 0 1</span><br><span class="line">1) <span class="string">"three"</span></span><br><span class="line">2) <span class="string">"two"</span></span><br><span class="line">127.0.0.1:6379&gt; RPUSH list <span class="string">"hello right"</span></span><br><span class="line">(<span class="built_in">integer</span>) 4</span><br><span class="line">127.0.0.1:6379&gt; LRANGE list 0 -1</span><br><span class="line">1) <span class="string">"hello3"</span></span><br><span class="line">2) <span class="string">"hello2"</span></span><br><span class="line">3) <span class="string">"hello1"</span></span><br><span class="line">4) <span class="string">"hello"</span></span><br><span class="line">5) <span class="string">"hello right"</span></span><br><span class="line"><span class="comment">########## 移除值 ##########</span></span><br><span class="line">127.0.0.1:6379&gt; LPOP list</span><br><span class="line"><span class="string">"three"</span></span><br><span class="line">127.0.0.1:6379&gt; RPOP list</span><br><span class="line"><span class="string">"right"</span></span><br><span class="line">127.0.0.1:6379&gt; LRANGE list 0 -1</span><br><span class="line">1) <span class="string">"two"</span></span><br><span class="line">2) <span class="string">"one"</span></span><br><span class="line"><span class="comment">########## 下标取值 ##########</span></span><br><span class="line">127.0.0.1:6379&gt; LINDEX list 0</span><br><span class="line"><span class="string">"two"</span></span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; LLEN list</span><br><span class="line">(<span class="built_in">integer</span>) 2</span><br><span class="line">127.0.0.1:6379&gt;</span><br><span class="line">127.0.0.1:6379&gt;</span><br><span class="line">127.0.0.1:6379&gt; LREM list 1 one</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; LRANGE list 0 -1</span><br><span class="line">1) <span class="string">"two"</span></span><br><span class="line"><span class="comment">########## 截取部分 ##########</span></span><br><span class="line">127.0.0.1:6379&gt; LTRIM list 1 2</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; LRANGE list 0 -1</span><br><span class="line">1) <span class="string">"hello2"</span></span><br><span class="line">2) <span class="string">"hello1"</span></span><br><span class="line"><span class="comment">########## 移除最后一个到新list ##########</span></span><br><span class="line">127.0.0.1:6379&gt; RPOPLPUSH list list1</span><br><span class="line"><span class="string">"hello1"</span></span><br><span class="line">127.0.0.1:6379&gt; LRANGE list1 0 -1</span><br><span class="line">1) <span class="string">"hello1"</span></span><br><span class="line"><span class="comment">########## update下标 ##########</span></span><br><span class="line">127.0.0.1:6379&gt; LSET list1 0  <span class="string">"hello set1"</span></span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; LRANGE list1 0 -1</span><br><span class="line">1) <span class="string">"hello set1"</span></span><br><span class="line">127.0.0.1:6379&gt; LSET list1 1 <span class="string">"hello set1"</span></span><br><span class="line">(error) ERR index out of range <span class="comment">### 不存在该值</span></span><br><span class="line"></span><br><span class="line"><span class="comment">########## 插入在某个元素中的前面或者后面（BEFORE/AFTER） ##########</span></span><br><span class="line">127.0.0.1:6379&gt; LINSERT list BEFORE <span class="string">"hello1"</span> <span class="string">"insert hello1"</span></span><br><span class="line">(<span class="built_in">integer</span>) 5</span><br><span class="line">127.0.0.1:6379&gt; LRANGE list1 0 -1</span><br><span class="line">(empty array)</span><br><span class="line">127.0.0.1:6379&gt; LRANGE list  0 -1</span><br><span class="line">1) <span class="string">"hello4"</span></span><br><span class="line">2) <span class="string">"hello3"</span></span><br><span class="line">3) <span class="string">"hello2"</span></span><br><span class="line">4) <span class="string">"insert hello1"</span></span><br><span class="line">5) <span class="string">"hello1"</span></span><br></pre></td></tr></table></figure>

<ul>
<li>总结： <ol>
<li><strong><em>List</em></strong> 是个链表，left、right都可以插入值，值是可以重复的</li>
<li>空链表也表示不存在</li>
<li>查询会慢，删除中间值效率会低</li>
</ol>
</li>
</ul>
<p><strong><em>Set: 集合</em></strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">########## 例子数据准备 ##########</span></span><br><span class="line"><span class="comment">########## 插入 ##########</span></span><br><span class="line">127.0.0.1:6379&gt; SADD myset <span class="string">"hello0"</span></span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; SADD myset <span class="string">"hello1"</span></span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; SADD myset <span class="string">"hello2"</span></span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; SADD myset <span class="string">"hello3"</span></span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line"><span class="comment">########## 读取 ##########</span></span><br><span class="line">127.0.0.1:6379&gt; SMEMBERS myset</span><br><span class="line">1) <span class="string">"hello3"</span></span><br><span class="line">2) <span class="string">"hello0"</span></span><br><span class="line">3) <span class="string">"hello2"</span></span><br><span class="line">4) <span class="string">"hello1"</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 是否存在</span></span><br><span class="line">127.0.0.1:6379&gt; SISMEMBER myset <span class="string">"hello0"</span> </span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; SISMEMBER myset <span class="string">"hello9"</span></span><br><span class="line">(<span class="built_in">integer</span>) 0</span><br><span class="line"><span class="comment"># set个数</span></span><br><span class="line">127.0.0.1:6379&gt; SCARD myset </span><br><span class="line">(<span class="built_in">integer</span>) 4</span><br><span class="line"><span class="comment"># 随机获取</span></span><br><span class="line">127.0.0.1:6379&gt; SRANDMEMBER myset 1 </span><br><span class="line">1) <span class="string">"hello3"</span></span><br><span class="line">127.0.0.1:6379&gt; SRANDMEMBER myset 1</span><br><span class="line">1) <span class="string">"hello1"</span></span><br><span class="line"><span class="comment">########## 移除 ##########</span></span><br><span class="line">127.0.0.1:6379&gt; SREM myset hello0</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; SMEMBERS myset</span><br><span class="line">1) <span class="string">"hello3"</span></span><br><span class="line">2) <span class="string">"hello2"</span></span><br><span class="line">3) <span class="string">"hello1"</span></span><br><span class="line"><span class="comment"># 随机移除</span></span><br><span class="line">127.0.0.1:6379&gt; SPOP myset</span><br><span class="line"><span class="string">"hello3"</span></span><br><span class="line">127.0.0.1:6379&gt; SPOP myset</span><br><span class="line"><span class="string">"hello2"</span></span><br><span class="line"><span class="comment">########## 移动元素 ##########</span></span><br><span class="line">127.0.0.1:6379&gt; SADD myset2 kuangshen</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; SMOVE myset2 myset kuangshen</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; SMEMBERS myset</span><br><span class="line">1) <span class="string">"kuangshen"</span></span><br><span class="line">2) <span class="string">"hello1"</span></span><br><span class="line"><span class="comment">########## 运算 ##########</span></span><br><span class="line">微博、B站，共同关注</span><br><span class="line">127.0.0.1:6379&gt; SADD key1 a</span><br><span class="line">127.0.0.1:6379&gt; SADD key1 b</span><br><span class="line">127.0.0.1:6379&gt; SADD key1 c</span><br><span class="line">127.0.0.1:6379&gt; SADD key2 c</span><br><span class="line">127.0.0.1:6379&gt; SADD key2 d</span><br><span class="line">127.0.0.1:6379&gt; SADD key2 e</span><br><span class="line">- 差集</span><br><span class="line">127.0.0.1:6379&gt; SDIFF key1 key2</span><br><span class="line">1) <span class="string">"a"</span></span><br><span class="line">2) <span class="string">"b"</span></span><br><span class="line">- 交集</span><br><span class="line">127.0.0.1:6379&gt; SINTER key1 key2</span><br><span class="line">1) <span class="string">"c"</span></span><br><span class="line">- 并集</span><br><span class="line">127.0.0.1:6379&gt; SUNION key1 key2</span><br><span class="line">1) <span class="string">"a"</span></span><br><span class="line">2) <span class="string">"b"</span></span><br><span class="line">3) <span class="string">"c"</span></span><br><span class="line">4) <span class="string">"d"</span></span><br><span class="line">5) <span class="string">"e"</span></span><br></pre></td></tr></table></figure>

<ul>
<li>总结：<ol>
<li><strong><em>Set</em></strong> 是个无序不重复集合</li>
<li>***底层是啥需要探究</li>
</ol>
</li>
</ul>
<p><strong><em>Hash（哈希）</em></strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">########## 例子数据准备 ##########</span></span><br><span class="line"><span class="comment">### 插入 &amp;&amp; 读取</span></span><br><span class="line">127.0.0.1:6379&gt; HSET myhash field1 kuangshen</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; HGET myhash field1</span><br><span class="line"><span class="string">"kuangshen"</span></span><br><span class="line"><span class="comment">### 批量操作</span></span><br><span class="line">127.0.0.1:6379&gt; HMSET myhash field1 hello field2 world</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; HMGET myhash field1 field2</span><br><span class="line">1) <span class="string">"hello"</span></span><br><span class="line">2) <span class="string">"world"</span></span><br><span class="line"><span class="comment">### 删获取全部</span></span><br><span class="line">127.0.0.1:6379&gt; HGETALL myhash</span><br><span class="line">1) <span class="string">"field1"</span></span><br><span class="line">2) <span class="string">"hello"</span></span><br><span class="line">3) <span class="string">"field2"</span></span><br><span class="line">4) <span class="string">"world"</span></span><br><span class="line"><span class="comment">### 删除</span></span><br><span class="line">127.0.0.1:6379&gt; HDEL myhash field1 </span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; HGETALL myhash</span><br><span class="line">1) <span class="string">"field2"</span></span><br><span class="line">2) <span class="string">"world"</span></span><br><span class="line"><span class="comment">### 长度</span></span><br><span class="line">127.0.0.1:6379&gt; HLEN myhash</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line"><span class="comment">### 是否存在</span></span><br><span class="line">127.0.0.1:6379&gt; HEXISTS myhash field1</span><br><span class="line">(<span class="built_in">integer</span>) 0</span><br><span class="line">127.0.0.1:6379&gt; HEXISTS myhash field2</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line"><span class="comment">### 获取所有的 键、值</span></span><br><span class="line">127.0.0.1:6379&gt; HKEYS myhash</span><br><span class="line">1) <span class="string">"field2"</span></span><br><span class="line">127.0.0.1:6379&gt; HVALS myhash</span><br><span class="line">1) <span class="string">"world"</span></span><br><span class="line"><span class="comment">### 自增</span></span><br><span class="line">127.0.0.1:6379&gt; HSET myhash field1 5</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; HINCRBY myhash field1 1</span><br><span class="line">(<span class="built_in">integer</span>) 6</span><br><span class="line"><span class="comment">### 不存在才set，反之</span></span><br><span class="line">127.0.0.1:6379&gt; HSETNX myhash field3 <span class="built_in">test</span></span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; HSETNX myhash field3 test1</span><br><span class="line">(<span class="built_in">integer</span>) 0</span><br></pre></td></tr></table></figure>

<ul>
<li>总结：<ol>
<li>Map 集合 k-v</li>
<li>跟string没有太大的区别，命令上看起来也差不多</li>
</ol>
</li>
</ul>
<p><strong><em>ZSet: 有序集合</em></strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">########## 来一组数据 ##########</span></span><br><span class="line"><span class="comment">### 插入 &amp;&amp; 读取</span></span><br><span class="line">127.0.0.1:6379&gt; ZADD myset 1 one</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; ZADD myset 2 two</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; ZRANGE myset 0 -1</span><br><span class="line">1) <span class="string">"one"</span></span><br><span class="line">2) <span class="string">"two"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">### 排序的例子</span></span><br><span class="line">7.0.0.1:6379&gt; ZADD salary 2500 xiaohong</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; ZADD salary 5000 zhangshan</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; ZADD salary 500 kuangshen</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; ZRANGE salary 0 -1 <span class="comment">## 从小到大</span></span><br><span class="line">1) <span class="string">"kuangshen"</span></span><br><span class="line">2) <span class="string">"xiaohong"</span></span><br><span class="line">3) <span class="string">"zhangshan"</span></span><br><span class="line">127.0.0.1:6379&gt; ZREVRANGE salary 0 -1 <span class="comment">## 从大到小</span></span><br><span class="line">1) <span class="string">"zhangshan"</span></span><br><span class="line">2) <span class="string">"kuangshen"</span></span><br><span class="line">127.0.0.1:6379&gt; ZRANGEBYSCORE salary -inf +inf</span><br><span class="line">1) <span class="string">"kuangshen"</span></span><br><span class="line">2) <span class="string">"xiaohong"</span></span><br><span class="line">3) <span class="string">"zhangshan"</span></span><br><span class="line">127.0.0.1:6379&gt; ZRANGEBYSCORE salary -inf 2500</span><br><span class="line">1) <span class="string">"kuangshen"</span></span><br><span class="line">127.0.0.1:6379&gt; ZRANGEBYSCORE salary -inf +inf WITHSCORES</span><br><span class="line">1) <span class="string">"kuangshen"</span></span><br><span class="line">2) <span class="string">"500"</span></span><br><span class="line">3) <span class="string">"xiaohong"</span></span><br><span class="line">4) <span class="string">"2500"</span></span><br><span class="line">5) <span class="string">"zhangshan"</span></span><br><span class="line">6) <span class="string">"5000"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">### 删除</span></span><br><span class="line">127.0.0.1:6379&gt; ZREM salary xiaohong</span><br><span class="line">(<span class="built_in">integer</span>) 1 </span><br><span class="line">127.0.0.1:6379&gt; ZRANGEBYSCORE salary -inf +inf WITHSCORES</span><br><span class="line">1) <span class="string">"kuangshen"</span></span><br><span class="line">2) <span class="string">"500"</span></span><br><span class="line">3) <span class="string">"zhangshan"</span></span><br><span class="line">4) <span class="string">"5000"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">### 获取个数</span></span><br><span class="line">127.0.0.1:6379&gt; ZCARD salary</span><br><span class="line">(<span class="built_in">integer</span>) 2</span><br><span class="line"></span><br><span class="line"><span class="comment">### 获取某个范围</span></span><br><span class="line">127.0.0.1:6379&gt; ZADD myset 1 hello</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; ZADD myset 2 world</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; ZADD myset 3 <span class="built_in">test</span></span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; ZCOUNT myset 1 3</span><br><span class="line">(<span class="built_in">integer</span>) 3</span><br><span class="line">127.0.0.1:6379&gt; ZCOUNT myset 1 2</span><br><span class="line">(<span class="built_in">integer</span>) 2</span><br><span class="line">127.0.0.1:6379&gt; ZCOUNT myset 2 3</span><br><span class="line">(<span class="built_in">integer</span>) 2</span><br><span class="line">127.0.0.1:6379&gt; ZCOUNT myset 0 0</span><br><span class="line">(<span class="built_in">integer</span>) 0</span><br></pre></td></tr></table></figure>

<ul>
<li><p>以上总结：</p>
<p>基本都是GET SET，不同场景用不同的数据结构，然后遇到不懂得去官方文档搜索</p>
</li>
</ul>
<h5 id="2-三种特殊数据类型"><a href="#2-三种特殊数据类型" class="headerlink" title="2. 三种特殊数据类型"></a>2. 三种特殊数据类型</h5><p><strong><em>geospatial</em></strong> (地理位置)</p>
<p>​    这个功能可以推算地理位置，两地之间的举例，方圆几里的人！官方文档上搜到，只有6个命令。</p>
<p><img src="D:%5Chill%5Cmyhexo%5Csource%5Cimages%5CRedis-%E5%9F%BA%E7%A1%80%5Cimage-20210401000317268.png" alt="image-20210401000317268"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">### 坐标有限制、不能输入两级</span></span><br><span class="line"><span class="comment">### GEOADD 经度、纬度、名称 （-180 ~ 180 -85 ~ 85）</span></span><br><span class="line">127.0.0.1:6379&gt; GEOADD china:city 113.27 23.15 guangzhou</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; GEOADD china:city 121.48 31.40 shanghai</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; GEOADD china:city 160.50 29.53 chongqing</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; GEOADD china:city 123.82 47.15 qiqihaer</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; GEOADD china:city 120.21 30.20 hangzhou</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line"></span><br><span class="line"><span class="comment">### 获取</span></span><br><span class="line">127.0.0.1:6379&gt; GEOPOS china:city guangzhou hangzhou</span><br><span class="line">1) 1) <span class="string">"113.27000051736831665"</span></span><br><span class="line">   2) <span class="string">"23.14999996266175941"</span></span><br><span class="line">2) 1) <span class="string">"120.21000176668167114"</span></span><br><span class="line">   2) <span class="string">"30.19999988833350102"</span></span><br><span class="line">   </span><br><span class="line"><span class="comment">### 返回距离 指定距离</span></span><br><span class="line">127.0.0.1:6379&gt; GEODIST china:city guangzhou hangzhou</span><br><span class="line"><span class="string">"1043777.5906"</span></span><br><span class="line">127.0.0.1:6379&gt; GEODIST china:city guangzhou hangzhou km</span><br><span class="line"><span class="string">"1043.7776"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">### 搜附近</span></span><br><span class="line">127.0.0.1:6379&gt; GEORADIUS china:city  110 30 1000 km</span><br><span class="line">1) <span class="string">"guangzhou"</span></span><br><span class="line">2) <span class="string">"hangzhou"</span></span><br><span class="line">127.0.0.1:6379&gt; GEORADIUS china:city  110 30 500 km</span><br><span class="line">(empty array)</span><br><span class="line">&lt;127.0.0.1:6379&gt; GEORADIUS china:city  110 30 1000 km WITHDIST</span><br><span class="line">1) 1) <span class="string">"guangzhou"</span></span><br><span class="line">   2) <span class="string">"828.2964"</span></span><br><span class="line">2) 1) <span class="string">"hangzhou"</span></span><br><span class="line">   2) <span class="string">"982.4071"</span></span><br><span class="line">127.0.0.1:6379&gt; GEORADIUS china:city  110 30 1000 km WITHCOORD</span><br><span class="line">1) 1) <span class="string">"guangzhou"</span></span><br><span class="line">   2) 1) <span class="string">"113.27000051736831665"</span></span><br><span class="line">      2) <span class="string">"23.14999996266175941"</span></span><br><span class="line">2) 1) <span class="string">"hangzhou"</span></span><br><span class="line">   2) 1) <span class="string">"120.21000176668167114"</span></span><br><span class="line">      2) <span class="string">"30.19999988833350102"</span>      </span><br><span class="line">127.0.0.1:6379&gt; GEORADIUS china:city  110 30 1000 km WITHCOORD COUNT 1</span><br><span class="line">1) 1) <span class="string">"guangzhou"</span></span><br><span class="line">   2) 1) <span class="string">"113.27000051736831665"</span></span><br><span class="line">      2) <span class="string">"23.14999996266175941"</span></span><br><span class="line">      </span><br><span class="line"><span class="comment">### 指定key的around</span></span><br><span class="line">&lt;127.0.0.1:6379&gt; GEORADIUSBYMEMBER china:city guangzhou 10000 km</span><br><span class="line">1) <span class="string">"guangzhou"</span></span><br><span class="line">2) <span class="string">"hangzhou"</span></span><br><span class="line">3) <span class="string">"shanghai"</span></span><br><span class="line">4) <span class="string">"chongqing"</span></span><br><span class="line">5) <span class="string">"qiqihaer"</span></span><br></pre></td></tr></table></figure>

<p><strong><em>Hypeloglog</em></strong> </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong><em>Bitmap</em></strong>（位图）</p>
<p>​    位存储，一个占一个BIt，开销比较小</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#### GET SET</span></span><br><span class="line">127.0.0.1:6379&gt; SETBIT sign 0 1</span><br><span class="line">(<span class="built_in">integer</span>) 0</span><br><span class="line">127.0.0.1:6379&gt; SETBIT sign  1 0</span><br><span class="line">(<span class="built_in">integer</span>) 0</span><br><span class="line">127.0.0.1:6379&gt; SETBIT sign  2 0</span><br><span class="line">(<span class="built_in">integer</span>) 0</span><br><span class="line">127.0.0.1:6379&gt; GETBIT sign 0</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; GETBIT sign 3</span><br><span class="line">(<span class="built_in">integer</span>) 0</span><br><span class="line"><span class="comment">### 统计</span></span><br><span class="line">127.0.0.1:6379&gt; BITCOUNT sign -1 -1</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; BITCOUNT sign</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br></pre></td></tr></table></figure>

<h4 id="三、Redis事务"><a href="#三、Redis事务" class="headerlink" title="三、Redis事务"></a>三、Redis事务</h4><ol>
<li>基本概念</li>
</ol>
<p>​        <strong><em>Redis</em></strong>的事务，不支持原子性，单条命令保证原子性。<strong><em>Redis</em></strong>事务的本质是：一组命令集合。一个事务的所有命令会被序列化，在事务执行过程中，会按照顺序执行！</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">### 特性：</span></span><br><span class="line"><span class="comment">###1. 一次性</span></span><br><span class="line"><span class="comment">###2. 顺序性</span></span><br><span class="line"><span class="comment">###3. 排他性</span></span><br><span class="line"><span class="comment">###4. 无隔离级别</span></span><br></pre></td></tr></table></figure>

<ol start="2">
<li>实际操作：</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#### 事务操作</span></span><br><span class="line">127.0.0.1:6379&gt; MULTI</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379(TX)&gt; SET k1 v1</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; SET k1 v2</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; SET k1 v3</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; EXEC</span><br><span class="line">1) OK</span><br><span class="line">2) OK</span><br><span class="line">3) OK</span><br><span class="line"></span><br><span class="line"><span class="comment">#### 取消事务</span></span><br><span class="line">127.0.0.1:6379&gt; MULTI</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379(TX)&gt; SET k1 v1</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; SET k2 v2</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; SET k4 v4</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; DISCARD</span><br><span class="line">OK</span><br></pre></td></tr></table></figure>

<ul>
<li><p>错误处理：</p>
<ol>
<li><p>编译型异常 -  （所有的命令都不执行）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; MULTI</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379(TX)&gt; <span class="built_in">set</span> k1 v1</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; <span class="built_in">set</span> k2 v2</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; <span class="built_in">set</span> k3 v3</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; GETSET k3 0</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; GETSET k3</span><br><span class="line">(error) ERR wrong number of arguments <span class="keyword">for</span> <span class="string">'getset'</span> <span class="built_in">command</span></span><br><span class="line">127.0.0.1:6379(TX)&gt; GETSET k4 v4</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; EXEC</span><br><span class="line">(error) EXECABORT Transaction discarded because of previous errors.</span><br><span class="line">127.0.0.1:6379&gt; get k5</span><br><span class="line">(nil)</span><br></pre></td></tr></table></figure>
</li>
<li><p>运行时异常 - （其他命令可以执行，错误命令抛出异常）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">27.0.0.1:6379&gt; <span class="built_in">set</span> k1 <span class="string">"v1"</span></span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; MULTI</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379(TX)&gt; INCR k1</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; <span class="built_in">set</span> k2 v2</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; <span class="built_in">set</span> k3 v3</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; EXEC</span><br><span class="line">1) (error) ERR value is not an <span class="built_in">integer</span> or out of range</span><br><span class="line">2) OK</span><br><span class="line">3) OK</span><br><span class="line">127.0.0.1:6379&gt; get k2</span><br><span class="line"><span class="string">"v2"</span></span><br></pre></td></tr></table></figure>
</li>
</ol>
</li>
</ul>
<ol start="3">
<li><p>监控</p>
<p><strong><em>悲观锁</em></strong></p>
<p>​        无论什么操作都加锁</p>
<p><strong><em>乐观锁</em></strong></p>
<p>​        读不加锁，写的时候判断是否之前被修改过</p>
<p><strong><em>Redis监控</em></strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; SET money 100</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; SET out 0</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; WATCH money</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; MULTI</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379(TX)&gt; DECRBY money 20</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; INCRBY money 20</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; EXEC</span><br><span class="line">1) (<span class="built_in">integer</span>) 80</span><br><span class="line">2) (<span class="built_in">integer</span>) 100</span><br><span class="line"></span><br><span class="line"><span class="comment">#### 终端1</span></span><br><span class="line">127.0.0.1:6379&gt; WATCH money</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; MULTI</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379(TX)&gt; DECRBY money 10</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; INCRBY out 10</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; EXEC <span class="comment">### 提交失败，乐观锁</span></span><br><span class="line">(nil)</span><br><span class="line"></span><br><span class="line"><span class="comment">#### 终端2</span></span><br><span class="line">127.0.0.1:6379&gt; GET money</span><br><span class="line"><span class="string">"100"</span></span><br><span class="line">127.0.0.1:6379&gt; <span class="built_in">set</span> money 1000</span><br><span class="line">OK</span><br><span class="line"></span><br><span class="line"><span class="comment">#### 上述解决办法</span></span><br><span class="line">127.0.0.1:6379&gt; UNWATCH</span><br><span class="line">127.0.0.1:6379&gt; WATCH money <span class="comment">#自旋锁，重新获取新的版本号比对</span></span><br></pre></td></tr></table></figure>
</li>
</ol>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kity@2.0.4/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kityminder-core@1.4.50/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.css">
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2021/03/30/%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91%E7%9B%B8%E5%85%B3/%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91%E5%AF%BB%E8%B7%AF%E6%80%BB%E7%BB%93/" rel="next" title="游戏开发相关/游戏开发寻路总结">
                <i class="fa fa-chevron-left"></i> 游戏开发相关/游戏开发寻路总结
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2021/04/21/Nosql/Redis-%E8%BF%9B%E9%98%B6/" rel="prev" title="Nosql/Redis-进阶">
                Nosql/Redis-进阶 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">heyaojia</p>
              <p class="site-description motion-element" itemprop="description">游戏行业的一枚小鲜肉</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">21</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">5</span>
                  <span class="site-state-item-name">categories</span>
                </a>
              </div>
            

            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="git@gitee.com:hillcool/hillcool.git" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-gitee"></i>GitHub</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#Redis-基础"><span class="nav-number">1.</span> <span class="nav-text">Redis-基础</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#一、准备"><span class="nav-number">1.1.</span> <span class="nav-text">一、准备</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#1-MacOS安装"><span class="nav-number">1.1.1.</span> <span class="nav-text">1. MacOS安装</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-官方自带的测试"><span class="nav-number">1.1.2.</span> <span class="nav-text">2. 官方自带的测试</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#二、Redis数据类型-Redis文档"><span class="nav-number">1.2.</span> <span class="nav-text">二、Redis数据类型 Redis文档</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#1-五大基本数据类型"><span class="nav-number">1.2.1.</span> <span class="nav-text">1. 五大基本数据类型</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-三种特殊数据类型"><span class="nav-number">1.2.2.</span> <span class="nav-text">2. 三种特殊数据类型</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#三、Redis事务"><span class="nav-number">1.3.</span> <span class="nav-text">三、Redis事务</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">heyaojia</span>

  

  
</div>









        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
























  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.0.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.0.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=6.0.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=6.0.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.0.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.0.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.0.0"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
